home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 2,801 to 2,900 / aol-file-protocol-4400-2801-to-2900.zip / AOLDLs / C++ Files Library / Acere (Card Game) / AcereÄ.sit / Acereƒ / CardDeck.cp < prev    next >
Text File  |  1994-08-24  |  5KB  |  223 lines

  1. #include "CardDeck.h"
  2. #include "AcereApp.h"
  3.  
  4. #define kNoCard -1
  5.  
  6. static    RGBColor    RGBwhite;
  7.  
  8. CardDeck    *theDeck;
  9.  
  10. CardDeck::CardDeck()
  11. {
  12.     GetDateTime((unsigned long *)&qd.randSeed);        //    initialize randomizer when we create a new deck object
  13.                                             //    since Generate doesn't do this, we only do it once per session
  14.                                         
  15.     ZapOldDeck();
  16.     GenerateNewDeck();
  17.     
  18.     nextCardPosition = 0;
  19.     
  20.     RGBwhite.red = RGBwhite.blue = RGBwhite.green = 0xffff;
  21. }
  22.  
  23. CardDeck::~CardDeck()
  24. {
  25. }
  26.  
  27.     
  28. void  CardDeck::GenerateNewDeck()
  29. {
  30. #define abs(x) (x) < 0 ? (-x) : (x)
  31.  
  32.     unsigned short    i = 0, tempShort;
  33.     Boolean        happy;
  34.     
  35.     for (i = 0; i < NumCards; i++)
  36.         theCardPositions[i] = -1;
  37.     
  38.     for (i = 0; i < NumCards; i++)
  39.     {
  40.         happy = false;
  41.         do
  42.         {
  43.             tempShort = abs(Random());
  44.             tempShort =  tempShort% NumCards;
  45.             if ((theCardPositions[tempShort] == -1) && (theCards[i] == -1))    //    card is unassigned & so is position
  46.             {
  47.                 happy = true;
  48.                 theCardPositions[tempShort] = i;
  49.                 theCards[i] = tempShort;
  50.             }
  51.         } while (!happy);
  52.     }    
  53. }
  54.  
  55. void  CardDeck::ZapOldDeck()        //    after we finish a game to cleanup before generating a new one
  56. {
  57.     short    i;
  58.     
  59.     for (i=0; i < NumCards; i++)
  60.     {
  61.         theCards[i] = -1;
  62.     }
  63. }
  64.  
  65.  
  66. void    CardDeck::DrawCard(CardStruct *whichCard, Rect theRect)
  67. {
  68.     Rect            tempRect;
  69.     RGBColor        theBackColor;
  70.     CIconHandle    theIcon;
  71.     short        saveFont, saveSize;
  72.     Str15        cardString;
  73.     
  74.     Boolean        fullCard = (theRect.bottom - theRect.top > 30);
  75.     
  76.     if (whichCard->card == kNoCard)
  77.         return;
  78.     
  79. //    GetCardInfo(whichCard, theCard);
  80.         
  81.      GetBackColor(&theBackColor);
  82.      RGBBackColor(&RGBwhite);
  83.      
  84.      if (fullCard)
  85.      {
  86.          EraseRoundRect(&theRect, 20, 20);
  87.          FrameRoundRect(&theRect, 20, 20);
  88.      }
  89.      else                                    //    we need to do PART of a rounded rectangle
  90.      {
  91.         DrawShortCardBoundary(theRect);
  92.      }
  93.  
  94.      tempRect.top = theRect.top + 3;
  95.      tempRect.left = theRect.left + 3;
  96.      tempRect.bottom = tempRect.top + 16;
  97.      tempRect.right = tempRect.left + 12;
  98.      
  99.      theIcon = theApp->suitPats[whichCard->suit][0];
  100.      
  101.      PlotCIcon(&tempRect,theIcon);
  102.  
  103.      saveFont = qd.thePort->txFont;
  104.      saveSize = qd.thePort->txSize;
  105.      
  106.      cardString[0] = 1;
  107.      
  108.      switch(whichCard->card)
  109.      {
  110.          case 1:
  111.              cardString[1] = 'A';
  112.              break;
  113.          case 11:
  114.              cardString[1] = 'J';
  115.              break;
  116.          case 12:
  117.              cardString[1] = 'Q';
  118.              break;
  119.          case 13:
  120.              cardString[1] = 'K';
  121.              break;
  122.          default:
  123.              NumToString((long)whichCard->card, cardString);
  124.              
  125.      }
  126.      
  127.      TextSize(12);
  128.      TextFont(systemFont);
  129.      
  130.      tempRect.left += 14;
  131.      MoveTo(tempRect.left, tempRect.bottom -4);
  132.      DrawString(cardString);
  133.      
  134.      if (fullCard)
  135.      {
  136.          TextSize(24);
  137.          tempRect.left = theRect.left + ((theRect.right - theRect.left)/2);
  138.          tempRect.top = theRect.top +  ((theRect.bottom - theRect.top)/2) + 6;
  139.          tempRect.left -= StringWidth(cardString)/2;
  140.          
  141.          MoveTo(tempRect.left, tempRect.top);
  142.          DrawString(cardString);
  143.          
  144.          tempRect.top = theRect.bottom - 19;
  145.          tempRect.left = theRect.right - 15;
  146.          tempRect.bottom = tempRect.top + 16;
  147.          tempRect.right = tempRect.left + 12;
  148.          
  149.          theIcon = theApp->suitPats[whichCard->suit][1];
  150.          
  151.          PlotCIcon(&tempRect,theIcon);
  152.      }
  153.      
  154.      //    restore everything
  155.      
  156.      TextSize(saveSize);
  157.      TextFont(saveFont);
  158.      
  159.      RGBBackColor(&theBackColor);
  160. }
  161.  
  162. short    CardDeck::GetNextCardPosition(void)
  163. {
  164.     nextCardPosition++;
  165.     if (nextCardPosition >= NumCards)
  166.         return (kNoCard);
  167.     
  168.     return ((short)nextCardPosition);
  169. }
  170.  
  171. short    CardDeck::GetPositionValue(short whichPosition)
  172. {
  173.     return ((short)theCardPositions[whichPosition]);
  174. }
  175.  
  176. void        CardDeck::GetCardInfo(short whichCard, CardStruct *cardInfo)
  177. {
  178.     if ((whichCard == kNoCard) || (whichCard < 0) || (whichCard > NumCards))
  179.     {
  180.         cardInfo->card = kNoCard;
  181.         return;
  182.     }
  183.     cardInfo->suit = whichCard / 13;
  184.     cardInfo->card = (whichCard % 13) + 1;
  185.     if ((cardInfo->suit == 0) || (cardInfo->suit == 3))
  186.         cardInfo->color = blackCard;
  187.     else
  188.         cardInfo->color = redCard;
  189. }
  190.  
  191. void    CardDeck::DrawShortCardBoundary(Rect theRect)
  192. {
  193.          Rect            tempRect = theRect;
  194.         RgnHandle        newRgn, oldRgn;
  195.          
  196.          
  197.          oldRgn = NewRgn();
  198.         OpenRgn();
  199.         tempRect.bottom +=20;
  200.         FrameRoundRect(&tempRect, 20, 20);
  201.         CloseRgn(oldRgn);
  202.         
  203.         newRgn = NewRgn();
  204.         OpenRgn();
  205.         tempRect.bottom += 22;
  206.         tempRect.top += 22;
  207.         FrameRoundRect(&tempRect, 20, 20);
  208.         CloseRgn(newRgn);
  209.         
  210. //        DiffRgn(newRgn,oldRgn,newRgn);
  211.         DiffRgn(oldRgn, newRgn, newRgn);
  212.         
  213. //        RectRgn(oldRgn,&theRect);
  214. //        SectRgn(newRgn,oldRgn,newRgn);
  215.         
  216. //        saveClipRgn = NewRgn();
  217.         
  218. //        GetClip(saveClipRgn);
  219. //        SetClip(newRgn);
  220.         EraseRgn(newRgn);
  221.         FrameRgn(newRgn);
  222. }
  223.